── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
library(leaflet)library(wbstats)#Download dataWBData <-wb_data(country ="countries_only", indicator =c("AG.LND.FRST.ZS", "AG.LND.FRST.K2"))# Load country data (long/lat)WBCountries <-wb_countries()clean_data <- WBData[-c(which(is.na(WBData$AG.LND.FRST.K2) |is.na(WBData$AG.LND.FRST.ZS))),]wb_dat <-merge(clean_data, y = WBCountries[c("iso2c", "longitude", "latitude")], by ="iso2c", all.x =TRUE)# Rename Columnscolnames(wb_dat)[5] <-'ForestedArea'colnames(wb_dat)[6] <-'PercentForested'wb_dat <- wb_dat[-c(which(is.na(wb_dat$longitude) |is.na(wb_dat$latitude))),]
The data used in this assignment come from the World Bank. There are two data sets which are being used. The proportion of a country that is forested, and also the total land area of the forests. Also included with world bank data are things like longitude and latitude information for capitals.
This first map places a single marker at the capital of each country, but I found it to be very difficult to read at smaller zoom levels, also some information seems quite wrong compared to raw data, so it is not enough information. I chose to use satellite images because the subject is forestry so why not?
Please note that rgdal will be retired by the end of 2023,
plan transition to sf/stars/terra functions using GDAL and PROJ
at your earliest convenience.
rgdal: version: 1.5-32, (SVN revision 1176)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.4.2, released 2022/03/08
Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/gdal
GDAL binary built with GEOS: FALSE
Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/proj
PROJ CDN enabled: FALSE
Linking to sp version:1.5-0
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
res <-readOGR(dsn = url)
Warning in readOGR(dsn = url): First layer countries1 read; multiple layers present in
https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json, check layers with ogrListLayers()
OGR data source with driver: TopoJSON
Source: "https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json", layer: "countries1"
with 180 features
It has 3 fields
This map appears to be superior to the previous map. Though it does not show explicit numbers on the map, it is a very nice looking example that shows general proportions of forestry in different countries. The country shape data came from https://github.com/deldersveld/topojson/blob/master/world-countries.json. I tried using the non-antarctic version but it wasn’t working for some strange reason.
make_ts_popup <-function(id){ggplot(filter(wb_dat, id == country)) +geom_line(aes(x=date, y = ForestedArea)) +theme_minimal() +ylab(expression(paste('Forested Area'," (km"^2,")"))) +xlab('Date') +ggtitle(paste0(filter(wb_dat, id == country)[1,"country"]))}plot_all <-lapply(wb_dat2$country, make_ts_popup)Marked_Map_GraphPopUp <-leaflet(wb_dat2) %>%setView(lng =0, lat =0, zoom =1) %>%addProviderTiles(providers$Esri.WorldImagery) %>%addAwesomeMarkers(~longitude, ~latitude,popup =popupGraph(plot_all, width =300, height =200),icon = icons)Marked_Map_GraphPopUp
This map was similar to the first one, but I decided to include popup graphs with time series that show how the area of forests have changed over time. The most interesting time series are in South America in my opinion. Most countries are heavily deforesting aside from Chile, and Uruguay. By far the most interesting thing about this is that Chile and Uruguay are the most developed nations in South America (they have the highest GDP per Capita) and have a different trend in forestation.
I thought that this would be the final map which combines the best elements of the two previous graphs. It has colored polygons for different proportions of land containing forests as well as small circular markers in capitals which can be clicked for the same popup graphs as before. I thought to make the circles the same color change as the rest of the countries but it was hard to find capitals if you don’t know where to look. I initially tried to get it mapped so the capital circles weren’t necessary, but after a few attempts at merging the time series with the spatial data, it just wasn’t loading properly.